package net.guerlab.smart.uploader.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.guerlab.smart.uploader.service.controller.AbstractUploadController;
import net.guerlab.web.result.Fail;
import net.guerlab.web.result.Result;
import net.guerlab.web.result.Succeed;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Base64;

/**
 * 上传处理
 *
 * @author guer
 */
@Api(tags = "文件上传")
@RestController(UploadController.BASE_PATH)
@RequestMapping(UploadController.BASE_PATH)
public class UploadController extends AbstractUploadController {

    public static final String BASE_PATH = DEFAULT_PATH;

    @ApiOperation("获取文件base64")
    @PostMapping("/base64")
    public Result<String> base64(MultipartFile file) {
        try {
            return new Succeed<>(Succeed.MSG, Base64.getEncoder().encodeToString(file.getBytes()));
        } catch (Exception e) {
            return new Fail<>(e.getLocalizedMessage());
        }
    }

    @Override
    protected String getBasePath() {
        return BASE_PATH;
    }
}
